package com.wzy.mall.service.admin.impl;

import com.wzy.mall.mapper.adminMapper.OrderMapper;
import com.wzy.mall.model.bo.adminBO.OrderByPageBO;
import com.wzy.mall.model.bo.adminBO.OrderChangeBO;
import com.wzy.mall.model.pojo.adminPOJO.OrderPOJO;
import com.wzy.mall.model.pojo.adminPOJO.SpecPOJO;
import com.wzy.mall.model.vo.adminVO.*;
import com.wzy.mall.service.admin.OrderService;
import com.wzy.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class OrderServiceImpl implements OrderService {

    public OrderLimitVO findOrderByPage(OrderByPageBO orderByPageBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);

        int currentPage = orderByPageBO.getCurrentPage();
        int pagesize = orderByPageBO.getPagesize();

        if(currentPage <= 0) {
            currentPage = 1;
        }
        // 计算开始索引
        int start = (currentPage - 1) * pagesize;

        // 查询符合条件的总记录数
        int totalCount = mapper.findTotalCount(orderByPageBO);

        OrderLimitVO orderLimitVO = null;
        try {
            List<OrderPageVO> orderPageVOList = mapper.findOrderByPage(orderByPageBO, start);
            System.out.println(orderPageVOList);

            orderLimitVO = new OrderLimitVO();
            orderLimitVO.setTotal(totalCount);
            orderLimitVO.setOrders(orderPageVOList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return orderLimitVO;
    }

    public OrderInfoVO getOrder(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);

        OrderInfoVO orderInfoVO = null;
        try {
            // 查询订单的基本信息
            orderInfoVO = mapper.findOrderByID(id);

            // 获取所有订单状态
            List<OrderStatesVO> orderStatesVOList = mapper.selectState();
            //System.out.println(orderStatesVOList);

            // 获取对应商品的所有规格
            List<SpecVO> specVOList = mapper.selectSpecByGoodsID(orderInfoVO.getGoodsDetailId());

            orderInfoVO.setStates(orderStatesVOList);
            orderInfoVO.setSpec(specVOList);

            //System.out.println(orderInfoVO);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return orderInfoVO;
    }

    public int deleteOrder(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);

        int result = 0;
        try {
            result = mapper.deleteOrderByID(id);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return result;
    }

    public int updateOrder(OrderChangeBO orderChangeBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);

        // 需要根据订单ID获取规格名、规格单价
        SpecPOJO specPOJO = mapper.selectSpecByID(orderChangeBO.getSpec());

        // 更改规格ID、规格名、规格单价
        OrderPOJO orderPOJO = new OrderPOJO();
        orderPOJO.setId(orderChangeBO.getId());
        orderPOJO.setSpecId(specPOJO.getId());
        orderPOJO.setSpecName(specPOJO.getSpec_name());
        orderPOJO.setSpecPrice(specPOJO.getUnitprice());
        // 更改总金额
        Double amount = specPOJO.getUnitprice() * orderChangeBO.getNum();
        orderPOJO.setAmount(amount);
        // 更改订单状态
        orderPOJO.setStateId(orderChangeBO.getState());
        orderPOJO.setGoodsNum(orderChangeBO.getNum());

        int result = 0;
        try {
            result = mapper.updateOrder(orderPOJO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return result;
    }
}
